package com.xgst.runningmanager

import android.app.AlertDialog
import androidx.activity.viewModels
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import com.luck.picture.lib.config.MediaType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.luck.picture.lib.model.PictureSelector
import com.xgst.baselib.BaseActivity
import com.xgst.runningmanager.databinding.ActivityErrorBinding
import com.xgst.runningmanager.databinding.ActivityInputWorkOrderBinding
import com.xgst.runningmanager.http.api.ErrorOrderAPI
import com.xgst.runningmanager.http.api.WorkOrderAPI
import com.xgst.runningmanager.viewmodel.InputViewModel
import java.io.File


class InputErrorActivity:BaseActivity<ActivityErrorBinding>() {

    private val eventTypes = arrayOf("衣着问题", "言语文明问题", "上班时间脱岗", "其他")

    private val model: InputViewModel by viewModels()
    private var fileUrl = ""
    override fun init() {
        binding.imgAdd.setOnClickListener {
            PictureSelector.create(this)
                .openGallery(MediaType.ALL)
                .setImageEngine(GlideEngine.createGlideEngine())
                .setMaxSelectNum(1)
                .forResult(object : OnResultCallbackListener {
                    override fun onCancel() {
                        LogUtils.i("取消了")
                    }

                    override fun onResult(result: List<LocalMedia>) {
                        val path = result[0].absolutePath
                        postService(path?:"/")
                    }
                })
        }
        binding.tvTime.text = TimeUtils.getNowString()

        binding.tvType.setOnClickListener {
            showSingleChoiceDialog()
        }

        binding.btnSubmit.setOnClickListener {

            //提交
            val tvType = binding.tvType.text.toString()
            val index = eventTypes.indexOf(tvType)
            val tvTime = binding.tvTime.text.toString()
            //描述信息
            val etQ = binding.etQ.text.toString()
            val etTitle = binding.etTitle.text.toString()

            val api = ErrorOrderAPI(
                category = index,
                submitUser = "李默",
                submitTime = tvTime,
                description = etQ,
                attachFile = fileUrl,
                title =  etTitle
            )

            model.submitE(api)
        }
    }

    private fun postService(path: String) {
        Glide.with(this)
            .load(path)
            .into(binding.imgAdd)

        model.postFile(File(path),{
            fileUrl = it
        })
    }

    private fun showSingleChoiceDialog() {
        var selectedEventType = eventTypes[0]

        val builder = AlertDialog.Builder(this)
        builder.setTitle("失范类型")
            .setSingleChoiceItems(eventTypes, 0) { _, which ->
                selectedEventType = eventTypes[which]
            }
            .setPositiveButton("确定") { _, _ ->
                binding.tvType.text = selectedEventType
            }
            .setNegativeButton("取消", null)

        builder.create().show()
    }
}